package com.itheima.store.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.itheima.store.dao.SystemSettingDao;
import com.itheima.store.domain.SystemSetting;
import com.itheima.store.utils.JDBCUtils;

public class SystemSettingDaoImpl implements SystemSettingDao {

	@Override
	public void saveSystemSetting(SystemSetting systemSetting) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql1 = "select * from sys_setting ";
		SystemSetting ss = queryRunner.query(sql1, new BeanHandler<SystemSetting>(SystemSetting.class));
		if(ss !=null){
			String sql = "update sys_setting  set bowCounts=?,rebowDays=? where sys_setting_id=?";
			queryRunner.update(sql, systemSetting.getBowCounts(),systemSetting.getRebowDays(),ss.getSys_setting_id());
		}else{
			String sql = "insert into sys_setting value(?,?,?)";
			queryRunner.update(sql, systemSetting.getSys_setting_id(),systemSetting.getBowCounts(),systemSetting.getRebowDays());

		}
		
		
	}

	@Override
	public SystemSetting findSystemSetting() throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql1 = "select * from sys_setting ";
		SystemSetting ss = queryRunner.query(sql1, new BeanHandler<SystemSetting>(SystemSetting.class));
		return ss;
	}

}
